﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.Common;
using System.Text;

namespace Maticsoft.Web.wap
{
    public partial class Settlement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Check();

            AjaxProcess();

            if (!IsPostBack)
            {
                BindAddress();
                BindOitem();
            }
        }

        protected void AjaxProcess( )
        {
            if (Request.QueryString["sync"] == "true")
            {
                StringBuilder strBuilder = new StringBuilder();
                BLL.ShoppingInfo carBLL = new BLL.ShoppingInfo();
                DataSet dsFactoryNo = carBLL.GetShoppingCarFactoryNo(DigitHelper.GetInt32(this.UserId));
                decimal totalMoney = 0;
                if (dsFactoryNo.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow drNo in dsFactoryNo.Tables[0].Rows)
                    {
                        DataSet ds = carBLL.GetListView("UserId=" + this.UserId + " and FactoryNo='" + drNo["factoryNo"]
                            + "' and IsActive=1 and ProductId in(" + Request.QueryString["ids"] + ")");
                        decimal productTotal = 0;
                        decimal deliveryFee = 0;
                        decimal insureFee = 0;
                        decimal taxFee = 0;
                        int productNum = 0;

                        decimal weight = 0;
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            productNum += DigitHelper.GetInt32(dr["ProductNum"]);
                            productTotal += DigitHelper.GetInt32(dr["ProductNum"]) * DigitHelper.GetDecimal(dr["SalePrice"]);
                            //if (DigitHelper.GetDecimal(dr["ShipFee"]) > 0 && DigitHelper.GetDecimal(dr["Weight"]) < 1000)
                            //{
                            //deliveryFee += DigitHelper.GetDecimal(dr["ShipFee"]);
                            //}
                            weight += DigitHelper.GetDecimal(dr["Weight"]) * DigitHelper.GetInt32(dr["ProductNum"]);
                            if (deliveryFee == 0)
                            {
                                deliveryFee = DigitHelper.GetDecimal(dr["ShipFee"]);
                            }
                        }
                        BLL.DeliveryAddr addrBLL = new BLL.DeliveryAddr();
                        Model.DeliveryAddr addrModel = addrBLL.GetModel(DigitHelper.GetInt32(Request.QueryString["delivery"]));
                        BLL.Orders orderBLL = new BLL.Orders();
                        Model.Orders orderModel = new Model.Orders();
                        //配送信息
                        orderModel.Mobile = addrModel.Mobile;
                        orderModel.Name = addrModel.Name;
                        orderModel.Address = addrModel.Address;
                        orderModel.PostCode = addrModel.PostCode;
                        orderModel.IdentityNo = addrModel.IdentityNo;
                        orderModel.IdentityFrontPic = addrModel.IdentityFrontPic;
                        orderModel.IdentityBackPic = addrModel.IdentityBackPic;
                        orderModel.LocationId = addrModel.LocationId;
                        //费用
                        //订单总重量小于1000克加运费
                        orderModel.DeliveryFee = weight < 1000 ? deliveryFee : 0;
                        orderModel.CouponFee = 0;
                        orderModel.InsureFee = insureFee;
                        orderModel.TaxFee = taxFee;
                        //价格
                        orderModel.OitemNum = productNum;
                        orderModel.ProductPrice = productTotal;
                        orderModel.Total = orderModel.DeliveryFee + orderModel.CouponFee + orderModel.InsureFee + orderModel.TaxFee + orderModel.ProductPrice;
                        //订单信息
                        orderModel.Status = 1;
                        orderModel.UserId = DigitHelper.GetInt32(this.UserId);
                        orderModel.CreateTime = DateTime.Now;
                        orderModel.IsActive = 1;
                        orderModel.OrderId = orderBLL.Add(orderModel);
                        BLL.Oitems oitemBLL = new BLL.Oitems();
                        Model.Oitems oitemModel = null;
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            oitemModel = new Model.Oitems();

                            oitemModel.DeliveryFee = DigitHelper.GetDecimal(dr["ShipFee"]);
                            oitemModel.InsureFee = DigitHelper.GetDecimal(dr["InsureFee"]);
                            oitemModel.TaxFee = DigitHelper.GetDecimal(dr["TaxFee"]);

                            oitemModel.RealPrice = DigitHelper.GetDecimal(dr["SalePrice"]);
                            oitemModel.SalePrice = DigitHelper.GetDecimal(dr["SalePrice"]);
                            oitemModel.MarketPrice = DigitHelper.GetDecimal(dr["MarketPrice"]);
                            oitemModel.TotalPrice = DigitHelper.GetDecimal(dr["SalePrice"]) * DigitHelper.GetDecimal(dr["ProductNum"]);

                            oitemModel.ProductDesc = dr["ProductDesc"].ToString();
                            oitemModel.ProductId = DigitHelper.GetInt32(dr["ProductId"]);
                            oitemModel.ProductName = dr["ProductName"].ToString();
                            oitemModel.ProductNum = DigitHelper.GetInt32(dr["ProductNum"]);
                            oitemModel.ProductPic = dr["Pic"].ToString();

                            oitemModel.OrderId = orderModel.OrderId;
                            oitemModel.CreateTime = DateTime.Now;
                            oitemModel.UpdateTime = DateTime.Now;
                            oitemModel.IsActive = 1;

                            oitemBLL.Add(oitemModel);
                            Model.ShoppingInfo carModel = carBLL.GetModel(DigitHelper.GetInt32(this.UserId), DigitHelper.GetInt32(dr["ProductId"]));
                            carModel.IsActive = 0;
                            carBLL.Update(carModel);
                        }

                        //总订单金额
                        totalMoney += (decimal)orderModel.Total;

                        if (strBuilder.ToString() != "")
                        {
                            strBuilder.Append("|");
                        }
                        strBuilder.Append(orderModel.OrderId);
                    }
                }

                Response.Write("{\"code\":1,\"msg\":\"\",\"data\":{\"total\":" + totalMoney + ",\"orderid\":" + strBuilder.ToString() + "}}");
                Response.End();
            }
        }

        protected void BindAddress( )
        {
            BLL.DeliveryAddr addrBLL = new BLL.DeliveryAddr();
            List<Model.DeliveryAddr> list = addrBLL.GetModelList("UserId=" + UserId + " and IsActive=1 order by DeliveryId desc");
            Repeater1.DataSource = list;
            Repeater1.DataBind();

            if (list.Count > 0)
            {
                hidDeliveryId.Value = list[0].DeliveryId.ToString();
            }
        }

        protected string GetLocation( object locationId )
        {
            int locId = DigitHelper.GetInt32(locationId);
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            Model.LocationInfo locModel = locBLL.GetModel(locId);
            if (locModel != null)
            {
                return locModel.pName + " " + locModel.cName;
            }

            return "";
        }

        protected void BindOitem( )
        {
            BLL.ShoppingInfo carBLL = new BLL.ShoppingInfo();
            DataSet ds = carBLL.GetListView("UserId=" + this.UserId + " and IsActive=1 and ProductId in(" + Request.QueryString["ids"] + ")");
            Repeater2.DataSource = ds.Tables[0].DefaultView;
            Repeater2.DataBind();
            decimal productTotal = 0;
            decimal deliveryFee = 0;
            decimal insureFee = 0;
            decimal taxFee = 0;
            int productNum = 0;


            DataSet dsFactoryNo = carBLL.GetShoppingCarFactoryNo(DigitHelper.GetInt32(this.UserId));
            if (dsFactoryNo.Tables.Count > 0 && dsFactoryNo.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow drr in dsFactoryNo.Tables[0].Rows)
                {
                    decimal currWeight = 0;
                    decimal currDeliveryFee = 0;
                    foreach (DataRow dr in ds.Tables[0].Select("FactoryNo='" + drr["FactoryNo"] + "'"))
                    {
                        productNum += DigitHelper.GetInt32(dr["ProductNum"]);
                        productTotal += DigitHelper.GetInt32(dr["ProductNum"]) * DigitHelper.GetDecimal(dr["SalePrice"]);

                        currWeight += DigitHelper.GetDecimal(dr["Weight"]) * DigitHelper.GetInt32(dr["ProductNum"]);
                        if (currDeliveryFee == 0)
                        {
                            currDeliveryFee = DigitHelper.GetDecimal(dr["ShipFee"]);
                        }
                    }
                    if (currWeight < 1000)
                    {
                        deliveryFee += currDeliveryFee;
                    }
                }
            }

            //ltlNum.Text = productNum.ToString();
            //ltlProductPrice.Text = productTotal.ToString("f2");
            ltlDeliveryFee.Text = deliveryFee.ToString("f2");
            ltlTotal.Text = (productTotal + deliveryFee).ToString("f2");
        }
    }
}